package com.business.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.business.system.dao.UserMapper;
import com.business.system.pojo.User;
import com.business.system.service.IUserSevice;
import com.utility.Page;

@Service
public class UserServiceImpl implements IUserSevice {

	@Autowired
	UserMapper userMapper;

	@Override
	public int insert(User user) throws RuntimeException {
		// TODO Auto-generated method stub
		user.setCreate_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date()));
		return this.userMapper.insert(user);
	}

	@Override
	public int update(User user) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.userMapper.update(user);
	}

	@Override
	public int delete(int id) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.userMapper.delete(id);
	}

	@Override
	public List<User> selectAll() throws RuntimeException {
		// TODO Auto-generated method stub
		return this.userMapper.selectAll();
	}

	@Override
	public int countAll() throws RuntimeException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public User findById(int id) throws RuntimeException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User findByAccountAndPassword(String account, String password)
			throws RuntimeException {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<String, String>();
		map.put("account", account);
		map.put("password", password);
		return this.userMapper.findUser(map);
	}

	@Override
	public User findByUserName(String userName) throws RuntimeException {
		return this.userMapper.findByUserName(userName);
	}

	@Override
	public Page<User> findUserListPage(User user, int pageNo, int pageSize) {

		Page<User> page = new Page<User>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", user.getUsername());
		map.put("account", user.getAccount());
		page.setParams(map);
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);

		List<User> list = this.userMapper.findUserListPage(page);
		page.setResults(list);
		return page;
	}
}
